package com.yxhl.zoume.core.tripsmgmt.presenter.unit;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.ICancelOrderPresenter;
import com.yxhl.zoume.core.tripsmgmt.view.unit.CancelOrderView;
import com.yxhl.zoume.data.http.rest.param.specialcar.CancelOrderParam;
import com.yxhl.zoume.data.http.rest.response.specialcar.CancelOrderResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.tripsmgmt.CancelOrderUseCase;

import javax.inject.Inject;


/**
 * Created by sugarya on 16/6/3.
 */
public class CancelOrderPresenter extends BasePresenter implements ICancelOrderPresenter {

    private static final String TAG = "TAG";

    @Inject
    CancelOrderUseCase mCancelOrderUseCase;

    private CancelOrderView mCancelOrderView;

    @Inject
    public CancelOrderPresenter(Context context) {
        super(context);
    }

    @Override
    public void cancelOrder(String orderId) {
        mCancelOrderView.waitingForOrderCancel(true);

        mCancelOrderUseCase.execute(new CancelOrderParam(getMobile(), getToken(), orderId),new ZMSubscriber<CancelOrderResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mCancelOrderView.waitingForOrderCancel(false);
                        mCancelOrderView.renderCancelError();
                    }

                    @Override
                    public void onNext(CancelOrderResponse cancelOrderResponse) {
                        super.onNext(cancelOrderResponse);
                        mCancelOrderView.waitingForOrderCancel(false);

                        if (cancelOrderResponse.isSucc()) {
                            mCancelOrderView.renderCancelOrderResult(true);
                        } else {
                            mCancelOrderView.renderCancelOrderResult(false);
                        }
                    }
                });
    }

    @Override
    public void attachView(BaseView view) {
        if (view != null) {
            mCancelOrderView = (CancelOrderView) view;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mCancelOrderUseCase.unsubscribe();
    }
}
